/**
 * uniapp vue2 vueX 文档
 * https://uniapp.dcloud.net.cn/tutorial/vue-vuex.html#state
 * 
 * 1. state
 * * * 获取state   this.$store.state.属性名
 * 2. getter 略（同计算属性）
 * 3. mutation
 * * * 设置state(传参)   this.$store.commit('SET_COUNT',10)
 * 4. action 异步操作，实际通过mutation改变数据
 */

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex); //vue的插件机制

//Vuex.Store 构造器选项
const store = new Vuex.Store({
	state:{
		version:'V0.3.9',    //版本号
		env:'prod',			 //develop - 测试环境；prod - 生产环境；c9 - 九灿项目
		
		develop:{
			httpDomain:'http://127.0.0.1:8182',
			resDomain:'https://res.gzhryc.com',
			appId:'wx710eb2150f69f015'
		},
		
		prod:{
			httpDomain:'https://busi.judaozhineng.com',  //域名
			resDomain:'https://res.gzhryc.com'  ,//资源域名
			appId:'wx2932cc0360d8e32b'
		},
		
		c9:{
			httpDomain:'https://busi.dongguanjiucan.com',  //域名
			resDomain:'https://res.dongguanjiucan.com'  ,//资源域名
			appId:'wx5edf1062ca211e60'
		},
		
		baseURL:'/agent',     //接口统一的前缀
		
		userInfo:{}          //登录用户信息，包含token
	},
	mutations:{
		SET_userInfo(state,data){
			state.userInfo = data;
			if(data){
				sessionStorage.setItem('session-userInfo',JSON.stringify(data));
			}else{
				sessionStorage.removeItem('session-userInfo');
			}
		},
		SET_wxCode(state,data){
			state.wxCode = data;
		}
    },
	getters:{
		GET_userInfo:state =>{
			let userInfo = state.userInfo;
			if(!userInfo || !userInfo.token){
				let str = sessionStorage.getItem('session-userInfo');
				if(str){
					userInfo = JSON.parse(str);
				}
			}
			return userInfo;
		},
		//获取请求域名
		GET_httpDomain:state =>{
			let httpDomain = state[state.env]?state[state.env].httpDomain:null;
			if(httpDomain){
				return httpDomain;
			}
			return window.location.protocol + '//' + window.location.host;
		},
		//获取资源域名
		GET_resDomain:state =>{
			let resDomain = state[state.env]?state[state.env].resDomain:'';
			return resDomain;
		},
		//获取appId
		GET_AppId:state =>{
			let appid = state[state.env]?state[state.env].appId:null;
			return appid;
		}
	}
})
export default store;